<template>
    <div>
        <Row style="margin:5px 0px 10px 5px" >
            <i-col span="2" >
                <Button  size="default"  type="primary" @click="btn_refresh">刷新</Button>
            </i-col>
		</Row>
        <i-table size="default"   border stripe ref="namespace_table" :columns="format" :data="show_list" :height="sharedState.tableHeight" >
            <template slot-scope="{ row, index }" slot="action">
                <Button  size="default"  type="error"  style="margin-left: 5px;"  @click="del_namespace(index)">删除</Button>
            </template>
        </i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
    </div>

</template>

<script>

import { k8s_cluster_post } from '@/api/http'
import { get_all_namespace_resources } from  '@/api'
import store from '@/store'

export default {
    data() {
        return {
            sharedState: store.state,
            format: [
                {
                    title: '命名空间',key: 'namespace'
                },
                {
                    title: 'CPU最低需求',key: 'resources',sortable:true,
                    sortMethod(a,b,type){
                        if(type=='asc'){
                            return a['requests']['cpu'] < b['requests']['cpu']  ? -1 : 1;
                        }else{
                            return a['requests']['cpu']  > b['requests']['cpu']  ? -1 : 1;
                        }
                    },
                    render: (h,params) =>{
                        let resources = params.row.resources
                        let requests = resources['requests']
                        let req_cpu = (requests['cpu']/1000).toFixed(2)+'c'
                        return h('pre',req_cpu)
                    }
                },
                {
                    title: 'CPU最高需求',key: 'resources',sortable:true,
                    sortMethod(a,b,type){
                        if(type=='asc'){
                            return a['limits']['cpu'] < b['limits']['cpu']  ? -1 : 1;
                        }else{
                            return a['limits']['cpu']  > b['limits']['cpu']  ? -1 : 1;
                        }
                    },
                    render: (h,params) =>{
                           let resources = params.row.resources
                        let limits = resources['limits']
                        let limit_cpu = (limits['cpu']/1000).toFixed(2)+'c'
                        return h('pre',limit_cpu)
                    }
                },
                {
                    title: '内存最低需求',key: 'resources',sortable:true,
                    sortMethod(a,b,type){
                        if(type=='asc'){
                            return a['requests']['memory'] < b['requests']['memory']  ? -1 : 1;
                        }else{
                            return a['requests']['memory']  > b['requests']['memory']  ? -1 : 1;
                        }
                    },
                    render: (h,params) =>{
                        let resources = params.row.resources
                        let requests = resources['requests']
                        let req_memory = (requests['memory']/1024).toFixed(2)+'G'
                        return h('pre',req_memory)
                    }
                },
                {
                    title: '内存最高需求',key: 'resources',sortable:true,
                    sortMethod(a,b,type){
                        if(type=='asc'){
                            return a['limits']['memory'] < b['limits']['memory']  ? -1 : 1;
                        }else{
                            return a['limits']['memory']  > b['limits']['memory']  ? -1 : 1;
                        }
                    },
                    render: (h,params) =>{
                        let resources = params.row.resources
                        let limits = resources['limits']
                        let limit_memory = (limits['memory']/1024).toFixed(2)+'G'
                        return h('pre',limit_memory)
                    }
                },
            ],
            total_list: [],
            show_list: [],
            // 分页
            total: 0,
            pageSize: 20,
        }
    },
    methods: {
        btn_refresh(){
            this.refresh()
        },
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        // bug 取消注入，refresh的页码并没有复原
        refresh() {
            k8s_cluster_post(get_all_namespace_resources).then( (response) => {
                this.total_list = response.data
                this.total = response.data.length
                if(this.total < this.pageSize) {
                    this.show_list = this.total_list
                }else {
                    let currentPage = this.$refs.page.currentPage
                    let _start = (currentPage-1) * this.pageSize
                    let _end = currentPage * this.pageSize
                    this.show_list = this.total_list.slice(_start,_end)
                }
            })
        },



    },
    mounted: function() {
        setTimeout(() =>{
            this.refresh();
        },200); 
    }
}
</script>
<style scoped>

</style>